package com.eattravel.controller;

import com.eattravel.pojo.Comment;
import com.eattravel.pojo.CommentCom;
import com.eattravel.service.CommentComService;
import com.eattravel.service.CommentService;
import com.eattravel.vo.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author glong
 * @date 2019/9/27 20:00
 * 描述：评论部分内容
 */
@RestController
@RequestMapping("comment")
public class CommentController {
    @Autowired
    CommentService commentService;

    @Autowired
    CommentComService commentComService;

    @RequestMapping("getComment")
    public Map<String, Object> getComment(String commodityId){
        System.out.println("商品号为："+commodityId);
        Map<String ,Object> modelMap = new HashMap<String ,Object>();
        List<Comment> comments = new ArrayList<>();
        // 取出查询到的数据
        try {
            comments = commentService.getCommentByFoodId(commodityId);
            modelMap.put("commentList", comments);
            modelMap.put("total",comments.size());
        } catch (Exception e){
            e.printStackTrace();
            modelMap.put("success",true);
            modelMap.put("errMsg",e.toString());
        }
        return modelMap;
    }

    @RequestMapping("addComment")
    public ResultData addComment(Comment comment) throws ParseException {
        ResultData rs = new ResultData();
        Date date = new Date(); //获得系统时间.
        SimpleDateFormat sdf = new SimpleDateFormat( " yyyy-MM-dd HH:mm:ss " );
        String nowTime = sdf.format(date);
        Date time = sdf.parse( nowTime );
        comment.setCommentHours(time);
        int result = commentService.addComment(comment);
        //System.out.println(comment.getId());
        if (result == 1){
            rs.setCode(1);
            rs.setData(comment);
            rs.setMessage("评论添加成功");
        }else {
            rs.setCode(0);
            rs.setMessage("评论添加失败");
        }
        return rs;
    }

    @RequestMapping("addComCom")
    public ResultData addCommentCom(CommentCom commentCom) throws ParseException {
        ResultData rs = new ResultData();
        Date date = new Date(); //获得系统时间.
        SimpleDateFormat sdf = new SimpleDateFormat( " yyyy-MM-dd HH:mm:ss " );
        String theTime = sdf.format(date);
        Date time = sdf.parse(theTime);
        commentCom.setComTime(time);
        // 返回结果
        int result = commentComService.addCommentCom(commentCom);
        if (result == 0 ){
            rs.setCode(0);
            rs.setMessage("评论添加失败");
        }else {
            rs.setCode(1);
            rs.setData(commentCom);
            rs.setMessage("评论添加成功");
        }

        return rs;
    }


}
